<?php 
/**
* 验证码类
*/
class Code
{
	public $cofing =array(
		'lenght'=>4,
		'width'=>200,
		'height'=>40,
		'line'=>0,
		'point'=>0,
		'fontsize'=>25,
		'fontfile'=>"./animeace.otf",
		'bgcolorrand'=>false,
		'str'=>"qwertyuiopasdfghjklzxcvbnm1234567890",
	);
	public $src;
	
	public  function __construct($cofing=array())
	{
		foreach ($cofing as $k => $v) {
			$this->cofing[$k]=$v;
		}
	}
	public function show()
	{
		// 1.发送HTTP头文件，声明内容为图像
		header("Content-type: image/png");

		// 2.创建画布

		$this->_createTrueColor();
		// 4.绘图(填充画布、画圆、画方块、画线条、画布上写字)

		$this->_point();

		$this->_line();

		$this->_fonts();

		// 5.输出图像
		imagepng($this->src);  


		// 6.释放图像资源

		imageDestroy($this->src); 

	}

	//创建画布
	public function _createTrueColor()
	{
		$this->src = imageCreateTrueColor($this->width,$this->height);
		if($this->bgcolorrand){
			$color = imageColorAllocate($this->src,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); 

			imageFill($this->src,0,0,$color);
		}else{
			$color = imageColorAllocate($this->src,255,255,255); 
			imageFill($this->src,0,0,$color);
		}
		
	}

	public function _point()
	{
		for ($i=0; $i <$this->point ; $i++) { 
			$color = imageColorAllocate($this->src,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));

			imagesetpixel ( $this->src , mt_rand(0,$this->width) , mt_rand(0,$this->height) , $color );
		}
	}

	public function _line()
	{
		for ($i=0; $i <$this->line ; $i++) { 
			$color = imageColorAllocate($this->src,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));

			imageline ( $this->src , mt_rand(0,$this->width) , mt_rand(0,$this->height/2) , mt_rand(0,$this->width) , mt_rand($this->height/2,$this->height) , $color );
		}

	}

	public function _fonts()
	{
		for ($i=0; $i <$this->lenght ; $i++) { 

			$color = imageColorAllocate($this->src,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));

			$x = $i*($this->width/$this->lenght);

			$y =($this->height/2)+($this->fontsize/2);

			imagettftext ( $this->src , $this->fontsize , mt_rand(-30,30) , $x , $y , $color , "./animeace.otf" , $this->str[mt_rand(0,strlen($this->str-1))]);
			
		}
	}



	public function __get($name)
	{
		return $this->cofing[$name];
	}

	public function __set($name,$value)
	{
		$this->cofing[$name]=$value;
	}
}

